package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.exception.OrderBusinessException;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserOrderMapper;
import com.sky.result.PageResult;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zcf
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private UserOrderMapper userOrderMapper;

    @Resource
    private OrderDetailMapper orderDetailMapper;
    @Resource
    private OrderMapper orderMapper;

    /**
     * 订单取消
     */
    @Override
    public void cancel(OrdersCancelDTO ordersRejectionDTO) {
        Orders order = userOrderMapper.getByID(ordersRejectionDTO.getId());
        //退款-暂时写死
        //然后更新订单
        order.setCancelReason(ordersRejectionDTO.getCancelReason());
        order.setCancelTime(LocalDateTime.now());
        order.setStatus(Orders.CANCELLED);
        userOrderMapper.update(order);
    }

    /**
     * 订单搜索
     *
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @param status
     * @param phone
     * @return
     */
    @Override
    public PageResult conditionSearch(int page, int pageSize, String number, String beginTime, String endTime, Integer status, String phone) {
        //1.设置参数
        PageHelper.startPage(page, pageSize);
        //2.查询
        List<Orders> list = orderMapper.list3(number, beginTime, endTime, status, phone);
        Page<Orders> orders = (Page<Orders>) list;
        List<OrderVO> orderVOList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (Orders order : orders) {
                OrderVO orderVO = new OrderVO();
                BeanUtils.copyProperties(order, orderVO);
                String orderDishesStr = getOrderDishesStr(order);
                orderVO.setOrderDishes(orderDishesStr);
                orderVOList.add(orderVO);
            }
        }
        return new PageResult(orders.getTotal(), orderVOList);
    }

    /**
     * 订单统计
     *
     * @return
     */
    @Override
    public OrderStatisticsVO statistics() {
        //统计数量
        Integer count1 = orderMapper.count(Orders.TO_BE_CONFIRMED);
        Integer count2 = orderMapper.count(Orders.CONFIRMED);
        Integer count3 = orderMapper.count(Orders.DELIVERY_IN_PROGRESS);
        OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO();
        orderStatisticsVO.setToBeConfirmed(count1);
        orderStatisticsVO.setConfirmed(count2);
        orderStatisticsVO.setDeliveryInProgress(count3);
        return orderStatisticsVO;
    }

    /**
     * 拒接
     *
     * @param ordersRejectionDTO
     */
    @Override
    public void rejection(OrdersRejectionDTO ordersRejectionDTO) {
        Orders order = userOrderMapper.getByID(ordersRejectionDTO.getId());
        order.setRejectionReason(ordersRejectionDTO.getRejectionReason());
        //只有待接单才可以拒单
        if (!Objects.equals(order.getStatus(), Orders.TO_BE_CONFIRMED)) {
            throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
        }
        //查询支付状态
        //退款
        //更新订单
        order.setRejectionReason(ordersRejectionDTO.getRejectionReason());
        order.setStatus(Orders.CANCELLED);
        order.setCancelTime(LocalDateTime.now());
        userOrderMapper.update(order);
    }


    /**
     * 接单
     *
     * @param ordersConfirmDTO
     */
    @Override
    public void confirm(OrdersConfirmDTO ordersConfirmDTO) {
        Orders order = userOrderMapper.getByID(ordersConfirmDTO.getId());
        if (!Objects.equals(order.getStatus(), Orders.TO_BE_CONFIRMED)) {
            throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
        }
        order.setStatus(Orders.CONFIRMED);
        userOrderMapper.update(order);
    }

    /**
     * 完成
     * @param id
     */
    @Override
    public void complete(Long id) {
        Orders order = userOrderMapper.getByID(id);
        //只有派送中的订单才可以完成
        if (!Objects.equals(order.getStatus(), Orders.DELIVERY_IN_PROGRESS)) {
            throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
        }
        order.setStatus(Orders.COMPLETED);
        order.setDeliveryTime(LocalDateTime.now());
        userOrderMapper.update(order);
    }

    /**
     * 查询订单
     * @param id
     * @return
     */
    @Override
    public OrderVO getById(Long id) {
        //1.查询订单
        Orders order = userOrderMapper.getByID(id);
        //2.查询订单明细
        List<OrderDetail> orderDetailList = orderDetailMapper.getByOrderId(order.getId());
        String orderDishesStr = getOrderDishesStr(order);
        //3.封装
        OrderVO orderVO = new OrderVO();
        BeanUtils.copyProperties(order, orderVO);
        orderVO.setOrderDishes(orderDishesStr);
        orderVO.setOrderDetailList(orderDetailList);
        return orderVO;
    }

    /**
     * 派送
     * @param id
     */
    @Override
    public void delivery(Long id) {
        Orders order = userOrderMapper.getByID(id);
        //只有待派送的订单才可以派送
        if (!Objects.equals(order.getStatus(), Orders.CONFIRMED)) {
            throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
        }
        order.setStatus(Orders.DELIVERY_IN_PROGRESS);
        userOrderMapper.update(order);
    }

    private String getOrderDishesStr(Orders orders) {
        List<OrderDetail> orderDetailList = orderDetailMapper.getByOrderId(orders.getId());
        List<String> stringList = orderDetailList.stream().map(
                orderDetail -> {
                    return orderDetail.getName() + "*" + orderDetail.getNumber();
                }

        ).collect(Collectors.toList());
        return String.join(",", stringList);
    }
}

